package com.xwj.back.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xwj.back.mapper.UserMapper;
import com.xwj.back.pojo.User;
import com.xwj.back.service.user.PersonalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class PersonalServiceImpl implements PersonalService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public User getUserById(Integer id) {
        User user = userMapper.selectById(id);
        user.setPassword("");
        return user;
    }

    @Override
    public String updateUserAvatar(Integer id, String photo) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id).set("photo",photo);
        userMapper.update(null,updateWrapper);
        return "success";
    }

    @Override
    public Map<String, String> updatePersonalInfo(Map<String, String> data) {
        Map<String, String> map = new HashMap<>();
        Integer id = Integer.valueOf(data.get("id"));
        User user = userMapper.selectById(id);
        String username = data.get("username");
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        Long count = userMapper.selectCount(queryWrapper);
        if(!user.getUsername().equals(username) && count >= 1){ //说明用户名重复
            map.put("message","repeat");
            return map;
        }
        String phone = data.get("phone");
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("username",username).set("phone",phone);
        userMapper.update(null,wrapper);
        map.put("message","success");
        return map;
    }

    @Override
    public Map<String, String> updatePersonalPassword(Map<String, String> data) {
        Integer id = Integer.valueOf(data.get("id"));
        User user = userMapper.selectById(id);
        String oldPassword = data.get("oldPassword");
        Map<String, String> map = new HashMap<>();
        if(passwordEncoder.matches(user.getPassword(),oldPassword)){
            map.put("message","old_error");
            return map;
        }
        String newPassword = data.get("newPassword");
        String confirmPassword = data.get("confirmPassword");
        if(!newPassword.equals(confirmPassword)){
            map.put("message","confirm_error");
            return map;
        }
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("id",id).set("password",passwordEncoder.encode(newPassword));
        userMapper.update(null,userUpdateWrapper);
        map.put("message","success");
        return map;
    }
}
